package com.vimi8.ebb.api.datareport;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.configuration.OAuth2AuthenticationImpl;
import com.vimi8.ebb.model.DataReport;
import com.vimi8.ebb.model.Error;
import com.vimi8.ebb.model.Paginator;
import com.vimi8.ebb.param.DataReportQueryParam;
import com.vimi8.ebb.service.DataReportService;
import com.vimi8.ebb.service.ElectricityBusinessService;
import com.vimi8.ebb.util.PagesUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

/**
 * 数据报告
 * Created by xiehuang on 2017/4/5.
 */

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-12T05:17:54.080Z")
@Controller
public class DataReportApiController implements DataReportApi {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger("LogisticsApiController");
    @Autowired
    private DataReportService dataReportService;
    @Autowired
    private ElectricityBusinessService electricityBusinessService;

    @Override
    public HttpResponseEntity<Paginator<List<DataReport>>> getDataReports(
            DataReportQueryParam param
    ) {
        //查询数据报告的条数
        int count = dataReportService.getDataReportCount(param);
        //分页查询数据报告
        List<DataReport> dataReports = dataReportService.getDataReports((param.getIndex() - 1) * param.getSize(), param.getSize(), param);
        //设置分页信息
        PagesUtil pagesUtil = new PagesUtil();
        pagesUtil.setPageSize(param.getSize());
        pagesUtil.setTotalCount(count);
        Paginator<List<DataReport>> paginator = new Paginator<List<DataReport>>(param.getIndex(), param.getSize(), pagesUtil.getTotalPage(), count, dataReports);
        return new HttpResponseEntity<Paginator<List<DataReport>>>(paginator, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<DataReport> getDataReportById(
            @ApiParam(value = "id", required = true) @PathVariable(value = "id", required = true) String id
    ) {
        DataReport dataReport = dataReportService.getDataReportById(id);
        return new HttpResponseEntity<DataReport>(dataReport, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<String> addDataReport(@ApiParam(value = "数据报告", required = true) @RequestBody DataReport dataReport) {
        int count = 0;
        try {
            dataReport.setId(UUID.randomUUID().toString());
            String addressCode = dataReport.getAddressCode();
            String orgClass = OAuth2AuthenticationImpl.ClientOrgClass();
            String clientAddressCode = null;
            // 根据电商办对应的区县 查询区县下的数据
            if ("org_ebb_city".equals(orgClass) || "org_ebb_county".equals(orgClass)) {
                clientAddressCode = electricityBusinessService.getClientAddressCode(OAuth2AuthenticationImpl.openId());
            }
            if (StringUtils.isEmpty(addressCode) && null != clientAddressCode && !"".equals(clientAddressCode)) {
                addressCode = clientAddressCode;
                logger.info("addressCode=" + addressCode);
            }
            dataReport.setAddressCode(addressCode);
            count = dataReportService.insertDataReport(dataReport);
        } catch (Exception e) {
            Error error = new Error().error("add error").errorDescription("add error");
            new HttpResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<String>(count + "", HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<String> updateDataReport(@ApiParam(value = "数据报告", required = true) @RequestBody DataReport dataReport) {
        int count = 0;
        try {
            String addressCode = dataReport.getAddressCode();
            String orgClass = OAuth2AuthenticationImpl.ClientOrgClass();
            String clientAddressCode = null;
            // 根据电商办对应的区县 查询区县下的数据
            if ("org_ebb_city".equals(orgClass) || "org_ebb_county".equals(orgClass)) {
                clientAddressCode = electricityBusinessService.getClientAddressCode(OAuth2AuthenticationImpl.openId());
            }
            if (StringUtils.isEmpty(addressCode) && null != clientAddressCode && !"".equals(clientAddressCode)) {
                addressCode = clientAddressCode;
                logger.info("addressCode=" + addressCode);
            }
            dataReport.setAddressCode(addressCode);
            logger.info("dataReport="+dataReport);
            count = dataReportService.updateDataReport(dataReport);
        } catch (Exception e) {
            Error error = new Error().error("update error").errorDescription("update error");
            new HttpResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<String>(count + "", HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<String> deleteDataReport(@ApiParam(value = "数据报告id", required = true) @RequestParam(value = "id", required = true) String id) {
        int count = 0;
        try {
            count = dataReportService.deleteDataReport(id);
        } catch (Exception e) {
            Error error = new Error().error("delete error").errorDescription("delete error");
            new HttpResponseEntity<Object>(error, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new HttpResponseEntity<String>(count + "", HttpStatus.OK);
    }

}
